package com.elective.courseselect.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elective.courseselect.dao.CharacterDao;
import com.elective.courseselect.dao.CourseCharacterDao;
import com.elective.courseselect.dao.CourseDao;
import com.elective.courseselect.entity.CourseCharacterEntity;
import com.elective.courseselect.enums.CharacterType;
import com.elective.courseselect.service.CourseCharacterService;
import com.elective.courseselect.utils.PageUtils;
import com.elective.courseselect.utils.Query;
import com.elective.courseselect.utils.R;
import com.elective.status.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;



@Service("courseCharacterService")
public class CourseCharacterServiceImpl extends ServiceImpl<CourseCharacterDao, CourseCharacterEntity> implements CourseCharacterService {

    @Autowired
    CharacterDao characterDao;

    @Autowired
    CourseDao courseDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CourseCharacterEntity> page = this.page(
                new Query<CourseCharacterEntity>().getPage(params),
                new QueryWrapper<CourseCharacterEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R addCourseCharacters(Map<String, Object> params) throws Exception {
        Integer courseId = (Integer) params.get("courseId");
        if (ObjectUtil.isNull(courseDao.selectById(courseId))) {
            throw new Exception("根据id查询系统发现没有此门课程,请检查");
        }
        List<Integer> characterIds = (List<Integer>) params.get("characterIds");
        for (Integer characterId : characterIds) {
            if (characterDao.selectById(characterId).getType() == CharacterType.CHARACTER_LABEL) {
                throw new Exception("添加的标签" + characterId + "不是课程类型标签");
                //return R.error(StatusCode.ERROR, "添加的标签" + characterId + "不是课程类型标签");
            }
            QueryWrapper<CourseCharacterEntity> courseCharacterEntityQueryWrapper = new QueryWrapper<>();
            courseCharacterEntityQueryWrapper.eq("character_id", characterId);
            courseCharacterEntityQueryWrapper.eq("course_id", courseId);
            if (super.list(courseCharacterEntityQueryWrapper).isEmpty()) {
                CourseCharacterEntity courseCharacterEntity = new CourseCharacterEntity();
                courseCharacterEntity.setCourseId(courseId);
                courseCharacterEntity.setCharacterId(characterId);
                super.save(courseCharacterEntity);
            }

        }
        return R.ok(StatusCode.OK,"添加课程标签成功");
    }

    @Override
    public R delCourseCharacters(Map<String, Object> params) throws Exception {
        Integer courseId = (Integer) params.get("courseId");
        if (ObjectUtil.isNull(courseDao.selectById(courseId))) {
            throw new Exception("根据id查询系统发现没有此门课程,请检查");
        }
        List<Integer> characterIds = (List<Integer>) params.get("characterIds");
        for (Integer characterId : characterIds) {
            QueryWrapper<CourseCharacterEntity> courseCharacterEntityQueryWrapper = new QueryWrapper<>();
            courseCharacterEntityQueryWrapper.eq("character_id", characterId);
            courseCharacterEntityQueryWrapper.eq("course_id", courseId);
            super.remove(courseCharacterEntityQueryWrapper);
        }
        return R.ok(StatusCode.OK,"删除课程标签成功");
    }

    @Override
    public R updateCourseCharacters(Map<String, Object> params) throws Exception {
        Integer courseId = (Integer) params.get("courseId");
        if (ObjectUtil.isNull(courseDao.selectById(courseId))) {
            throw new Exception("根据id查询系统发现没有此门课程,请检查");
        }
        QueryWrapper<CourseCharacterEntity> courseCharacterEntityQueryWrapper = new QueryWrapper<>();
        courseCharacterEntityQueryWrapper.eq("course_id", courseId);
        this.remove(courseCharacterEntityQueryWrapper);

        List<Integer> characterIds = (List<Integer>) params.get("characterIds");
        for (Integer characterId : characterIds) {
            if (characterDao.selectById(characterId).getType() == CharacterType.CHARACTER_LABEL) {
                throw new Exception("添加的标签" + characterId + "不是课程类型标签");
                //return R.error(StatusCode.ERROR, "添加的标签" + characterId + "不是课程类型标签");
            }
            CourseCharacterEntity courseCharacterEntity = new CourseCharacterEntity();
            courseCharacterEntity.setCourseId(courseId);
            courseCharacterEntity.setCharacterId(characterId);
            super.save(courseCharacterEntity);
        }
        return R.ok(StatusCode.OK, "添加课程标签成功");
    }

}